package com.aws.remote;

import com.amazonaws.AmazonClientException;
import com.amazonaws.SdkClientException;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.Upload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.Objects;

/**
 * @description:
 * @author: libugu@cskaoyan.com
 * @date 2024/1/17 - 14:25
 */
@Component
public class RemoteDemo {

    @Autowired
    AmazonS3 s3Client;
    String bucketName = "mylibugu";
    String localFolderPath = "C:\\Users\\libugu\\Desktop\\2024";
    String localFolderPath3 = "C:\\Users\\libugu\\Desktop\\2027";
    String localFolderPath2 = "/tmp";
    String s3Prefix = "";
    String folderPrefix = "tomc"; // 文件夹前缀
    String folderKeyword = "12";

    public void test() throws InterruptedException {
        TransferManager transferManager = new TransferManager(s3Client);

        String s3Prefix = new File(localFolderPath).getName(); // 使用最后一层文件夹名称作为根目录名称
        uploadFolder(transferManager, bucketName, new File(localFolderPath), s3Prefix);

        transferManager.shutdownNow();
        System.out.println("迁移完成");
    }

    private static void uploadFolder(TransferManager transferManager, String bucketName, File folder, String prefix) {
        for (File file : Objects.requireNonNull(folder.listFiles())) {
            if (file.isFile()) {
                Upload upload = transferManager.upload(bucketName, prefix + "/" + file.getPath().replace("\\", "/").substring(file.getPath().indexOf(prefix) + prefix.length() + 1), file);
                try {
                    upload.waitForCompletion();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else if (file.isDirectory()) {
                uploadFolder(transferManager, bucketName, file, prefix);
            }
        }
    }


    public void test2() {
        TransferManager transferManager = new TransferManager(s3Client);

        uploadFolder2(transferManager, bucketName, new File(localFolderPath2), folderPrefix);

        transferManager.shutdownNow();
    }

    private static void uploadFolder2(TransferManager transferManager, String bucketName, File folder, String folderPrefix) {
        for (File file : Objects.requireNonNull(folder.listFiles())) {
            if (file.isFile()) {
                Upload upload = transferManager.upload(bucketName, file.getPath().replace("\\", "/"), file);
                try {
                    upload.waitForCompletion();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else if (file.isDirectory() && file.getName().startsWith(folderPrefix)) {
                uploadFolder2(transferManager, bucketName, file, folderPrefix);
            }
        }
    }


    public void test3() {
        try {
            TransferManager transferManager = new TransferManager(s3Client);

            uploadFolder3(transferManager, bucketName, new File(localFolderPath3), folderKeyword);

            transferManager.shutdownNow();
            System.out.println("迁移完成");
        } catch (Exception e) {
            System.out.println("迁移失败");
            throw new RuntimeException(e);
        }
    }

    private static void uploadFolder3(TransferManager transferManager, String bucketName, File folder, String folderKeyword) {
        if (folder.isDirectory() && folder.getName().contains(folderKeyword)) {
            for (File file : Objects.requireNonNull(folder.listFiles())) {
                if (file.isFile()) {
                    Upload upload = transferManager.upload(bucketName, file.getPath().replace("\\", "/"), file);
                    try {
                        upload.waitForCompletion();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else if (file.isDirectory()) {
                    uploadFolder3(transferManager, bucketName, file, folderKeyword);
                }
            }

        }
    }
}
